﻿using System.Threading;
using MbUnit.Framework;
using Microsoft.SharePoint;

namespace CodePlex.Lekman.SharePoint.CacheDependencies.Tests
{
    /// <summary>
    /// Contains unit tests for the <see cref="SPListItemCacheDependency"/> class.
    /// </summary>
    [TestFixture]
    public class SPListItemCacheDependencyTestFixture : TestFixtureBase
    {
        /// <summary>
        /// Ensures that the class can be instantiated.
        /// </summary>
        [Test]
        public void TestInstantiate()
        {
            Assert.IsNotNull(new SPListItemCacheDependency(Web.Lists[0].Items[0]));
        }

        /// <summary>
        /// Tests that the changed event fires when the list properties has changed.
        /// </summary>
        [Test]
        public void TestItemHasChanged()
        {
            SPListItem item = Web.Lists[0].Items[0];
            SPListItemCacheDependency dependency = new SPListItemCacheDependency(item,
                                                                                 1000);

            //Now change the title property of the list
            item["Title"] = item["Title"];
            item.Update();

            //Wait for the dependency to detect the change
            Thread.Sleep(10000);
            Assert.IsTrue(dependency.HasChanged);
        }
    }
}